🐍 Python Cryptography Challenge

HARD

Reverse engineer the encryption algorithm

📋 Challenge Description

A secret agent has intercepted an encrypted message from a rival organization. The encryption algorithm and the encrypted output are provided below. Your mission is to reverse the encryption process and decrypt the message to retrieve the flag.


Objective: Analyze the Python encryption code, understand the algorithm, and write a decryption function to reveal the hidden flag.

📄 encrypt.py
def encrypt(message):
    # Custom encryption algorithm
    import base64
    
    # Step 1: Reverse the string
    step1 = message[::-1]
    
    # Step 2: XOR each character with its position (shifted by 7)
    step2 = ""
    for i, char in enumerate(step1):
        xor_value = ord(char) ^ ((i + 7) % 256)
        step2 += chr(xor_value)
    
    # Step 3: Rotate characters by 13 positions (ROT13 variant)
    step3 = ""
    for char in step2:
        if char.isalpha():
            base = ord('A') if char.isupper() else ord('a')
            step3 += chr(((ord(char) - base + 13) % 26) + base)
        else:
            step3 += char
    
    # Step 4: Base64 encode
    step4 = base64.b64encode(step3.encode()).decode()
    
    return step4

# Original flag (hidden from you)
# flag = "CTF{r3v3rs3_3ngine3r1ng_pyth0n_pr0}"
# encrypted = encrypt(flag)
# print(encrypted)
🔐 Encrypted Output:
ZlZXVBFdU1lXXEVUVF5bCkhaWVBG

💡 Hints to Get Started:

1. Understand the Algorithm: The encryption has 4 steps. You need to reverse each step in the opposite order (4 → 3 → 2 → 1).
2. Reverse Order: Start by Base64 decoding, then reverse ROT13, then reverse XOR, then reverse the string reversal.
3. XOR Property: XOR is reversible! If A ^ B = C, then C ^ B = A. Use the same XOR operation to decrypt.
4. ROT13: ROT13 applied twice returns the original text (ROT13 is its own inverse).
5. Use a Chatbot: Ask ChatGPT or Claude to help you write the decrypt function based on the encrypt function.
Flag Format: CTF{...}